package com.jeomo.sys.provider.web;

import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.annocation.DataDict;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.sys.api.enums.UserStatusEnum;
import com.jeomo.sys.api.vo.UserVo;
import com.jeomo.sys.provider.request.CreateUserRequest;
import com.jeomo.sys.starter.entity.User;
import com.jeomo.sys.starter.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author QBT
 */
@RestController
@RequestMapping("sys/user")
public class UserController extends BaseController {

    @Resource
    IUserService userService;

    @DeleteMapping("{code}")
    @ApiOperation("删除用户")
    public CommonResult<Boolean> remove(@PathVariable String code) {
        userService.removeById(code);
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("")
    public CommonResult<UserVo> create(@RequestBody CreateUserRequest request) {
        User user = BeanCopyUtil.copyProperties(request, User.class);
        userService.save(user, request.getRoles());
        UserVo vo = BeanCopyUtil.copyProperties(user, UserVo.class);
        return CommonResult.success(vo);
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    @PostMapping("page")
    @DataDict
    public CommonResult<PageResponseVo> page(@RequestBody PageRequest request) {
        PageResponseDto pageResponseDto = userService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        PageResponseVo userVos = BeanCopyUtil.copyPageProperties(pageResponseDto, UserVo.class);
        return CommonResult.success(userVos);
    }

    /**
     * 根据用户名查询会员的所有信息，包含角色信息
     * @param code
     * @return
     */
    @GetMapping("/{code}")
    @DataDict
    public CommonResult<UserVo> query(@PathVariable String code) {
        User user = userService.getById(code);
        if(user == null) {
            return CommonResult.failure("找不到的用户信息");
        }
        UserVo vo = BeanCopyUtil.copyProperties(user, UserVo.class);
        return CommonResult.success(vo);
    }

    public static void main(String[] args) {
        User user = new User();
        user.setStatus(UserStatusEnum.NORMAL);

        UserVo vo = BeanCopyUtil.copyProperties(user, UserVo.class);


        System.out.println(vo.getStatus());
    }

}
